<template>
  <div class="search">
    <div class="search-wrap">
      <div class="s-crumb">
        <span>全部商品</span>
        <span class="symbol search-item">></span>
        <span class="search-item">杯子</span>
      </div>
      <div class="search-content">
        <div class="sortBar">
          <div class="category">
            <span class="title">分类：</span>
            <a href="">居家生活</a>
            <a href="">美食酒水</a>
            <a href="">服饰鞋包</a>
            <a href="">母婴亲子</a>
            <a href="">个护清洁</a>
            <a href="">数码家电</a>
            <a href="">运动旅行</a>
            <a href="">文娱特色</a>
            <a href="">原创设计</a>
          </div>
          <div class="rank">
            <span class="title">分类：</span>
            <span
              class="rank-item"
              @click="changeDefault"
              :style="{ color: rankNum === 0 ? '#B4A078' : '#333' }"
              >默认</span
            >
            <span
              class="price rank-item"
              @click="changePrice"
              :style="{ color: rankNum === 1 ? '#B4A078' : '#333' }"
              >价格
            </span>
            <span
              class="decrement"
              @click="changePriceRank"
              :style="{
                borderBottomColor:
                  priceRank === 'decrement' ? '#B4A078' : '#A5AFA7',
              }"
            ></span>
            <span
              class="increment"
              @click="changePriceRank"
              :style="{
                borderTopColor:
                  priceRank === 'increment' ? '#B4A078' : '#A5AFA7',
              }"
            ></span>
            <form class="form" action="##" @submit.prevent="btnSearchPrice">
              <div class="input">
                <span>￥</span>
                <input
                  v-model="price.floorPrice"
                  @focus="isShowButton = true"
                  type="text"
                />
              </div>
              <div>-</div>
              <div class="input">
                <span>￥</span>
                <input
                  v-model="price.upperPrice"
                  @focus="isShowButton = true"
                  type="text"
                />
              </div>
              <div class="buttonDiv" v-show="isShowButton">
                <button class="submit" ref="submit">确定</button>
                <span @click="resetPrice">清空</span>
              </div>
            </form>
          </div>
        </div>
        <ul class="search-list">
          <li
            class="search-item"
            v-for="item in filterSearchList"
            :key="item.id"
          >
            <a href="">
              <div class="img-wrap">
                <img
                  v-for="(imgUrl, index) in item.imgUrlList"
                  :key="imgUrl.id"
                  :class="`img${index + 1}`"
                  :src="imgUrl.url"
                />
              </div>
              <div class="item-info">
                <h3>{{ item.title }}</h3>
                <p>
                  <span
                    >￥<span class="priceNum">{{
                      item.discountPrice
                    }}</span></span
                  >
                  <span class="defaultPrice" v-show="item.defaultPrice"
                    >￥<span class="priceNum">{{
                      item.defaultPrice
                    }}</span></span
                  >
                </p>
                <div class="addCart" v-if="!item.desc">加入购物车</div>
                <div v-else>
                  <hr />
                  <h4>{{ item.desc }}</h4>
                </div>
              </div>
            </a>
          </li>
        </ul>
        <div class="pagination">
          <div class="pagination-wrap">
            <div class="page-prev" @click="toPrev">
              <span :style="{ color: pageNo === 1 ? '#D6CCD6' : '#333' }"
                >&lt; 上一页</span
              >
            </div>
            <div
              class="page-item"
              v-show="startAndEnd.start > 1"
              @click="sendPageNo(1)"
              :class="{ active: pageNo === 1 }"
            >
              <span>1</span>
            </div>
            <div class="page-item" v-show="startAndEnd.start > 2">
              <span>...</span>
            </div>
            <div
              class="page-item"
              v-for="(item, index) in continues"
              :key="index"
              @click="sendPageNo(startAndEnd.start + index)"
              :class="{ active: pageNo === startAndEnd.start + index }"
            >
              <span>{{ startAndEnd.start + index }}</span>
            </div>
            <div class="page-item" v-show="totalPage - 1 > startAndEnd.end">
              <span>...</span>
            </div>
            <div
              class="page-item"
              v-show="totalPage > startAndEnd.end"
              @click="sendPageNo(totalPage)"
              :class="{ active: pageNo === totalPage }"
            >
              <span>{{ totalPage }}</span>
            </div>
            <div class="page-next" @click="toNext">
              <span
                :style="{ color: pageNo === totalPage ? '#D6CCD6' : '#333' }"
                >下一页 &gt;</span
              >
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { searchPriceReg } from '@/utils/searchPriceReg'
import { reqGetSearchListData } from '@/api/index'
export default {
  name: 'Search',
  data() {
    return {
      searchList: [],
      rankNum: 0,
      priceRank: undefined,
      price: {
        floorPrice: '',
        upperPrice: '',
      },
      isShowButton: false,
      pageNo: 1,
      filterSearchList: [],
      searchBtn: 1,
    }
  },
  mounted: async function () {
    const result = await reqGetSearchListData()
    this.searchList = result.data
    this.filterSearchList = this.searchList.slice(
      (this.pageNo - 1) * 4,
      this.pageNo * 4
    )
  },
  methods: {
    // 1.默认搜索排序
    changeDefault() {
      this.rankNum = 0
      this.priceRank = undefined
    },
    // 2.价格排序
    changePrice() {
      this.rankNum = 1
      this.priceRank = 'decrement'
    },
    // 3.更改价格排序方向
    changePriceRank(e) {
      if (e.target.className === 'decrement') {
        this.rankNum = 1
        this.priceRank = 'decrement'
      } else {
        this.rankNum = 1
        this.priceRank = 'increment'
      }
    },
    // 4.提交表单函数
    btnSearchPrice() {
      this.searchBtn++
      if (this.price.floorPrice && this.price.upperPrice) {
        if (this.price.floorPrice > this.price.upperPrice) {
          let floorPrice = this.price.upperPrice
          this.price.upperPrice = this.price.floorPrice
          this.price.floorPrice = floorPrice
        }
      }
      this.isShowButton = false
      //searchList
      // if (this.price.floorPrice) {
      //   this.filterSearchList = this.filterSearchList.filter(
      //     (item) => item.discountPrice >= this.price.floorPrice
      //   )
      // }
      // if (this.price.upperPrice) {
      //   this.filterSearchList = this.filterSearchList.filter(
      //     (item) => item.discountPrice <= this.price.upperPrice
      //   )
      // }
    },
    // 5.清空价格方法
    resetPrice() {
      this.price.floorPrice = ''
      this.price.upperPrice = ''
      this.isShowButton = false
      this.filterSearchList = this.searchList.slice(
      (this.pageNo - 1) * 4,
      this.pageNo * 4
    )
    },
    // 6.跳转到对应页面
    sendPageNo(pageNo) {
      this.pageNo = pageNo
      this.filterSearchList = this.searchList.slice(
        (this.pageNo - 1) * 4,
        this.pageNo * 4
      )
    },
    // 7.点击去上一页
    toPrev() {
      if (this.pageNo > 1) {
        this.pageNo = this.pageNo - 1
        this.filterSearchList = this.searchList.slice(
          (this.pageNo - 1) * 4,
          this.pageNo * 4
        )
      } else {
        return
      }
    },
    // 8.点击去下一页
    toNext() {
      if (this.pageNo < this.totalPage) {
        this.pageNo = this.pageNo + 1
        this.filterSearchList = this.searchList.slice(
          (this.pageNo - 1) * 4,
          this.pageNo * 4
        )
      } else {
        return
      }
    },
  },
  watch: {
    // 监听搜索的价格变化，改变旁边确定清除样式
    price: {
      handler: function (newPrice) {
        // 价格输入框正则验证，只能输入数字
        if (!searchPriceReg.test(newPrice.floorPrice)) {
          this.price.floorPrice = ''
        }
        if (!searchPriceReg.test(newPrice.upperPrice)) {
          this.price.upperPrice = ''
        }
        // 输入价格后旁边确定框样式改变
        if (newPrice.floorPrice || newPrice.upperPrice) {
          this.$refs.submit.style.color = '#b4a078'
        } else {
          this.$refs.submit.style.color = '#ccc'
        }
        this.price.floorPrice = newPrice.floorPrice.trim()
        this.price.upperPrice = newPrice.upperPrice.trim()
      },
      deep: true,
    },
    searchBtn: {
      handler() {
        //改变的时候 获取 minPice  maxPrice
        if (this.price.floorPrice) {
          this.filterSearchList = this.searchList.filter(
            (item) => item.discountPrice >= this.price.floorPrice
          )
        }
        if (this.price.upperPrice) {
          this.filterSearchList = this.searchList.filter(
            (item) => item.discountPrice <= this.price.upperPrice
          )
        }
      },
      deep: true,
    },
  },
  computed: {
    total() {
      return this.searchList.length
    },
    continues() {
      if (this.total < 12) {
        return Math.ceil(this.total / 4)
      } else {
        return 3
      }
    },
    totalPage() {
      return Math.ceil(this.total / 4)
    },
    startAndEnd() {
      let { pageNo, continues, totalPage } = this
      let start, end
      if (totalPage <= continues) {
        start = 1
        end = totalPage
      } else {
        start = pageNo - parseInt(continues / 2)
        end = pageNo + parseInt(continues / 2)
        if (start < 1) {
          start = 1
          end = continues
        } else if (end > totalPage) {
          end = totalPage
          start = end - continues + 1
        }
      }
      return { start, end }
    },
  },
}
</script>

<style lang="less" scoped>
.search {
  background-color: #f5f5f5;
  padding-bottom: 70px;
  .search-wrap {
    width: 1090px;
    margin: 0 auto;
    .s-crumb {
      height: 12px;
      padding: 24px 0;
      .symbol {
        margin: 0 5px;
      }
      .search-item {
        color: #9a9d9c;
      }
    }
    .search-content {
      overflow: hidden;
      background-color: #fff;
      padding: 0 40px;
      box-sizing: border-box;
      .sortBar {
        font-size: 14px;
        margin-top: 10px;
        .title {
          margin-right: 28px;
          color: #999;
        }
        a {
          margin-right: 30px;
          color: #333;
        }
        .category {
          color: #333;
          padding: 20px 0 16px;
          border-bottom: 1px dashed #dedede;
        }
        .rank {
          position: relative;
          padding: 20px 0;
          border-bottom: 1px solid #eaeaea;
          .rank-item {
            margin-right: 30px;
            color: #333;
            cursor: pointer;
          }
          .decrement {
            cursor: pointer;
            position: absolute;
            top: 18px;
            left: 165px;
            width: 0;
            height: 0;
            border: 5px solid #a5afa7;
            border-color: transparent transparent #a5afa7 transparent;
          }
          .increment {
            cursor: pointer;
            position: absolute;
            top: 30px;
            left: 165px;
            width: 0;
            height: 0;
            border: 5px solid #a5afa7;
            border-color: #a5afa7 transparent transparent transparent;
          }
          .form {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            left: 190px;
            margin-left: 15px;
            div {
              float: left;
              height: 27px;
              line-height: 27px;
              margin-right: 10px;
            }
            .input {
              display: flex;
              width: 65px;
              height: 26px;
              border: 1px solid #ccc;

              span {
                line-height: 26px;
                color: #d7d7db;
              }
              input {
                width: 48px;
                outline: none;
                border: none;
              }
            }
            .buttonDiv {
              .submit {
                cursor: pointer;
                width: 60px;
                height: 28px;
                margin-right: 10px;
                border: 1px solid #ececec;
                background-color: #f5f5f5;
                color: #ccc;
                border-radius: 2px;
              }
              span {
                cursor: pointer;
                color: #999;
              }
            }
          }
        }
      }
      .search-list {
        display: flex;
        flex-wrap: wrap;
        width: 1010px;
        margin-top: 20px;
        background-color: #fff;
        .search-item {
          width: 245px;
          height: 401px;
          margin-bottom: 50px;
          margin-right:7px;
          .img-wrap {
            position: relative;
            width: 100%;
            height: 245px;
            img {
              position: absolute;
              width: 245px;
              height: 245px;
            }
            .img1 {
              z-index: 1;
            }
            .img2 {
              z-index: 2;
            }
          }
          .item-info {
            width: 100%;
            hr {
              width: 130px;
              margin: 6px auto 14px;
              border-top: 1px solid #e8e8e8;
            }
            h3 {
              width: 166px;
              margin: 31px auto 4px;
              color: #333;
              text-align: center;
            }
            h4 {
              font-weight: normal;
              font-size: 14px;
              text-align: center;
              color: #999;
              width: 225px;
              height: 18px;
              line-height: 18px;
              margin: 0 10px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
            p {
              height: 21px;
              line-height: 21px;
              color: #d4282d;
              text-align: center;
              .defaultPrice {
                color: #999;
                text-decoration: line-through;
              }
              .priceNum {
                margin: 0 3px 0 -2px;
              }
            }
            .addCart {
              width: 108px;
              height: 28px;
              margin: 10px auto;
              line-height: 28px;
              font-size: 14px;
              color: #fff;
              text-align: center;
              border: 1px solid #b4a078;
              background-color: #b4a078;
              border-radius: 2px;
            }
          }
        }
        .search-item:hover {
          background-color: #f4f0ea;
          box-shadow: 1px 1px 8px rgb(0 0 0 / 20%);
        }
        .search-item:hover a {
          text-decoration: none;
        }
        .search-item:hover .img1 {
          z-index: 3;
        }
      }
      .pagination {
        margin-bottom: 80px;
        text-align: center;
        .pagination-wrap {
          display: inline-block;
          overflow: hidden;
          .active {
            background-color: #b4a078;
            span {
              color: #fff;
            }
          }
          > div {
            cursor: pointer;
            float: left;
            height: 38px;
            line-height: 38px;
            padding: 0 12px;
            border-top: 1px solid #e6e6e6;
            border-bottom: 1px solid #e6e6e6;
            border-left: 1px solid #e6e6e6;
            span:hover {
              text-decoration: none;
            }
          }
          .page-item:hover span {
            color: #b4a078;
          }
          .page-next {
            border-right: 1px solid #e6e6e6;
          }
        }
      }
    }
  }
}
</style>
